import {Document, Progress, UserGroup} from '../../models'
import * as $notify from '../../services/notification'

module.exports = {
  title: '给老师发通知',
  enable: true,
  async run ({versionId, progressId, lastProgressState}) {
    if (['check', 'done'].indexOf(lastProgressState) >= 0) return
    let progress = await Progress.findById(progressId)
    if (!progress) return
    let document = await Document.findById(versionId)
    let groupId = progress.groupId
    let userId = progress.userId
    let ug = await UserGroup.findOne({
      where: {
        userId,
        groupId
      }
    })
    if (!ug || !ug.name) return
    let title = document ? document.name : ''
    let ownerId = document ? document.userId : 0
    if (!title || !ownerId) return
    let url = 'pages/progress/main?isShowSideMenu=y&progressId=' + progressId
    if (progress.type === 'exercise') {
      if (!progress.documentId) return
      if (!progress.groupId) return
      url = `pages/homework-detail/main?agid=${progress.documentId}_${progress.groupId}&isShowSideMenu=y`
    }
    await $notify.sendNotification({
      title: '作业待批改提醒',
      data: {
        keyword1: {
          value: title
        },
        keyword2: {
          value: ug.name
        }
      },
      url
    }, {
      userId: ownerId
    })
  }
}
